package com.atguigu.service.impl;

import com.atguigu.mapper.CategoryMapper;
import com.atguigu.pojo.Category;
import com.atguigu.service.CategoryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program:weiblog-server
 * @author: 马雨清
 * @Time: 2023/8/20  21:55
 * @description:
 */
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查找所有文章分类
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public List<Category> findAllCategory() {
        List<Category> categoryList = categoryMapper.selectAllCategory();
        if (null != categoryList){
            return categoryList;
        }
        return null;
    }

    @Override
    public Integer addCategory(Category category) {
        Integer result = categoryMapper.insertCategory(category);
        return result;
    }

    @Override
    public Map<String, Object> findByPage(Integer pageNum, Integer pageSize, Category category) {
        PageHelper.startPage(pageNum,pageSize);
        List<Category> list = categoryMapper.selectByPage(category);
        PageInfo<Category> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int page = pageInfo.getPageNum();
        int limit = pageInfo.getPageSize();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("page",page);
        map.put("limit",limit);
        map.put("list",list);
        return map;
    }
}
